package cn.hackcloud.annotation.config;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;

import cn.hackcloud.annotation.bean.Color;
import cn.hackcloud.annotation.bean.ColorFactoryBean;
import cn.hackcloud.annotation.bean.Person;
import cn.hackcloud.annotation.bean.Red;
import cn.hackcloud.annotation.condition.LinuxCondition;
import cn.hackcloud.annotation.condition.MyImportBeanDefinitionRegistrar;
import cn.hackcloud.annotation.condition.MyImportSelector;
import cn.hackcloud.annotation.condition.WindowsCondition;

/**
 * 配置类
 * 
 * @author Created by laijun on 2018/10/19
 */
@Conditional({ WindowsCondition.class })
@Configuration
@Import({ Color.class, Red.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class }) // 导入组件，id是默认的全类名
@ImportResource
public class MainConfig2 {
	/**
	 * @see ConfigurableBeanFactory#SCOPE_PROTOTYPE
	 * @see ConfigurableBeanFactory#SCOPE_SINGLETON
	 * @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST
	 * @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION
	 */
	@Lazy
	@Scope
	@Bean(value = "person")
	public Person person() {
		System.out.println("给容器添加person");
		return new Person("hefang", 25);
	}

	/**
	 * @Conditional({}) :按照一定的条件给容器中注册bean 如果系统是windows给容器注册windows
	 *                  如果系统是linux给容器注册linux
	 */

	@Bean("windows")
	public Person person01() {
		return new Person("windows", 25);
	}

	@Conditional({ LinuxCondition.class })
	@Bean("linux")
	public Person person02() {
		return new Person("linux", 25);
	}

	@Bean
	public ColorFactoryBean colorFactoryBean() {
		return new ColorFactoryBean();
	}
}
